#!/bin/sh

# ----------------------------------------------------------------------
# Filename:   Net.sh
# Version:    2.0
# Date:       2013/09/22
# Author:     huijing.hei
# Email:      huijing.hei@cs2c.com.cn
# Summary:    rsh to rsh-server
# Notes:      Include EchoInfo and EchoResult
# Copyright:  China Standard Software Co., Ltd.
# History：     
#             Version 1.0, 2013/09/22
#             - The first one
# 
#             Version 1.1, 2013/11/08
#             - The second one
#             - Add service functions, start_daemon, stop_daemon, restart_daemon, status_daemon
#
#             Version 1.2, 2013/11/27
#             - Add SSHRemote, SCPRemote
#             - Add CheckServiceExists
# ----------------------------------------------------------------------

##! @TODO: redirect rsh to /usr/bin/rsh
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
export RSH="/usr/bin/rsh"

##! @TODO: redirect rcp to /usr/bin/rcp
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
export RCP="/usr/bin/rcp"


#################################
#
# check command exists
#
#################################
function exists()
{
    local RC=0
    for cmd in $*; do
        if ! command -v $cmd >/dev/null 2>&1; then
            echo -e "\033[31m FAIL\033[0m | $cmd not exists, pls check"
            RC=$((RC+1))
        fi
    done
    return $RC

}


#################################
#
# check user exists
#
#################################
function user_exists()
{
    if id $1  >/dev/null 2>&1; then
        return 0
    else
        echo "user $1 not exists, pls check"
        return 1
    fi
}


#################################
#
# start, stop, restart service
#
#################################
if command -v systemctl >/dev/null 2>&1; then
    HAVE_SYSTEMCTL=1
else
    HAVE_SYSTEMCTL=0
fi

##! @TODO: start service
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
function start_daemon()
{
        if [ $HAVE_SYSTEMCTL -eq 1 ]; then
                systemctl start $1.service > /dev/null 2>&1
        else
                service $1 start > /dev/null 2>&1
        fi

}

function stop_daemon()
{
        if [ $HAVE_SYSTEMCTL -eq 1 ]; then
                systemctl stop $1.service > /dev/null 2>&1
        else
                service $1 stop > /dev/null 2>&1
        fi


}

function status_daemon()
{
        if [ $HAVE_SYSTEMCTL -eq 1 ]; then
                systemctl status $1.service > /dev/null 2>&1
        else
                service $1 status > /dev/null 2>&1
        fi
}

function restart_daemon()
{
        if [ $HAVE_SYSTEMCTL -eq 1 ]; then
                systemctl restart $1.service > /dev/null 2>&1
        else
                service $1 restart > /dev/null 2>&1
        fi


}

#################
#ssh
#################
function SSHRemote()
{
    REMOTE="${1}"
    REMOTE_PASSWORD="${2}"
    EXEC_COMMAND="${3}"

        expect <<-END
        spawn ssh -n $REMOTE "${EXEC_COMMAND}"
            expect {
                "yes/no" { 
                     send "yes\r" 
                     exp_continue
                     }
                "password: " { 
                     send "$REMOTE_PASSWORD\r"
                     expect "Permission denied" { send_user "ssh $REMOTE failed\r"; exit 1; }
                     } 
                "WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!" { send_user "ssh failed, pls check /root/.ssh/known_hosts\n"; exit 1; }
                "No route to host" { send_user "ssh failed \n"; exit 1; }
            }
END

    return 0

}

#################
#scp
#################
function SCPRemote()
{
    REMOTE="${1}"
    REMOTE_PASSWORD="${2}"
    TMP_FILE="${3}"
    DST_FILE="${4}"

     expect <<-END
        spawn scp ${TMP_FILE} root@$REMOTE:${DST_FILE}
            expect {
                "yes/no" { 
                     send "yes\r" 
                     exp_continue
                     }
                "password: " { 
                     send "$REMOTE_PASSWORD\r"
                     expect "Permission denied" { send_user "ssh $REMOTE failed\r"; exit 1; }
                     }
                "No route to host" { send_user "ssh failed \n"; exit 1; }
            }
END

    return 0
}

##! @TODO: Check Service file exists
##!        
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => PASS; 1 => FAIL 
function CheckServiceExists()
{
    SERVFILE=""

    if [ $HAVE_SYSTEMCTL -eq 1 ]; then
        SERVFILE="/usr/lib/systemd/system/$1.service"
    else
        SERVFILE="/etc/init.d/$1"
    fi

    if [ ! -f ${SERVFILE} ]; then
        echo "${SERVFILE} not exists, pls check"
        return 1
    fi

    unset SERVFILE

    return 0

}

##! @TODO: Add user, and passwd user
##!        Arg1: user name
##!        Arg2: password 
##!        Arg3: -g
##!        Arg4: group
##!        Usage: AddUserPasswd username password -g group
##!        Usage: AddUserPasswd username password 
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => PASS; 1 => FAIL 
function AddUserPasswd()
{
    USER=$1 
    PASSWORD=$2
    

    # check user exists, if exists then delete
    id -ru ${USER} > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        userdel -r ${USER}
        if [ $? -ne 0 ];then
            echo "${USER} del failed."
            exit 1
        fi
    fi

    # check $3 is -g
    if [ "$3" = "-g" ];then
        GROUP=$4
        # group
        useradd -m ${USER} -g ${GROUP}
    else
        useradd -m ${USER}
    fi

    if [ $? -ne 0 ];then
        echo "${USER} add failed."
        exit 1
    fi

     expect <<-END
        spawn passwd ${USER}
            expect  {
                -re "(N|n)ew password:"  { 
                    send "${PASSWORD}\r"
                    exp_continue
                }

                "Unknown user name" {
                    send_user "${USER} not exists\n"
                    exit 1
                }

                "Weak password: too short." {
                    send_user "Your password is too short\n"
                    exit 1
                }
            }


        catch wait result
        exit [lindex \$result 3]

END

}


##! @TODO: Check user is root
##!        
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => PASS; 1 => FAIL 
function IsRoot()
{
    [ "x$(id -ru $1)" = "x0" ]

}
